<link rel="stylesheet" href="/static/admin/js/jconfirm/jquery-confirm.min.css">
<!--页面主要内容-->
<main class="lyear-layout-content">

    <div class="container-fluid">

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-toolbar clearfix">
                        <form class="pull-right search-bar" method="get" role="form">
                            <div class="input-group">
                                <div class="input-group-btn">
                                    <input type="hidden" name="search_field" id="search-field" value="username">
                                    <button class="btn btn-default dropdown-toggle" id="search-btn"
                                        data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                                        {$search_field == '用户名' ? '用户名' : '昵称'} <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="username">用户名</a>
                                        </li>
                                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="nickname">昵称</a>
                                        </li>
                                    </ul>
                                </div>
                                <input type="text" class="form-control" name="keyword" placeholder="{$keyword}">
                            </div>
                        </form>
                        <div class="toolbar-btn-action">
                            <a class="btn btn-primary m-r-5" href="{:url('admin/admin/add')}"><i
                                    class="mdi mdi-plus"></i> 新增</a>
                            <a class="btn btn-success m-r-5" href="javascript:void(0)"><i class="mdi mdi-check"></i>
                                启用</a>
                            <a class="btn btn-warning m-r-5" href="javascript:void(0)"><i
                                    class="mdi mdi-block-helper"></i> 禁用</a>
                            <a class="btn btn-danger" href="javascript:void(0)"><i class="mdi mdi-window-close"></i>
                                删除</a>
                        </div>
                    </div>
                    <div class="card-body">

                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>
                                            <label class="lyear-checkbox checkbox-primary">
                                                <input type="checkbox" id="check-all"><span></span>
                                            </label>
                                        </th>
                                        <th>编号</th>
                                        <th>用户名</th>
                                        <th>昵称</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {foreach($AdminList as $item)}
                                    <tr>
                                        <td>
                                            <label class="lyear-checkbox checkbox-primary">
                                                <input type="checkbox" name="ids[]" value="{$item.id}"><span></span>
                                            </label>
                                        </td>
                                        <td>{++$sun}</td>
                                        <td>{$item.username}</td>
                                        <td>{$item.nickname}</td>
                                        <td>
                                            <!-- <font class="text-success">{$item.state == 1 ? '正常' : '禁用' }</font> -->
                                            <font class="{$item.state == 1 ? 'text-success' : 'text-warning'} state">
                                                {$item.state == 1 ? '正常' : '禁用' }</font>
                                        </td>

                                        <td>
                                            <div class="btn-group">
                                                <a class="btn btn-xs btn-default btn-edit"
                                                    href="{:url('admin/admin/edit', ['id' => $item['id']])}" title="编辑"
                                                    data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>

                                                <a class="btn btn-xs btn-default btn-del" href="#!" data-id="{$item.id}"
                                                    title="删除" data-toggle="tooltip"><i
                                                        class="mdi mdi-window-close"></i></a>

                                            </div>
                                        </td>
                                    </tr>
                                    {/foreach}
                                </tbody>
                            </table>
                        </div>
                        <ul class="pagination">
                            {$AdminList->render()}
                        </ul>

                    </div>
                </div>
            </div>

        </div>

    </div>

</main>
<!--End 页面主要内容-->
<script src="/static/admin/js/jconfirm/jquery-confirm.min.js"></script>
<script src="/static/admin/js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="/static/admin/js/lightyear.js"></script>
<script type="text/javascript">
    $(function () {
        $('.search-bar .dropdown-menu a').click(function () {
            var field = $(this).data('field') || '';
            $('#search-field').val(field);
            $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
        });
        // 获取id方法
        function getID() {
            // 定义空的字符串
            var ids = '';

            // 遍历选中的复选框
            $('input[name="ids[]"]:checked').each(function () {
                // 拼接
                ids += $(this).val() + ',';
            });

            // 返回值
            return ids;
        }
        // 启用
        $('.btn-success').on('click', function () {
            $.alert({
                title: '修改管理员状态',
                content: '确认启用',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {
                            // 调用获取id方法
                            var ids = getID();

                            $.ajax({
                                type: 'post',
                                url: `{:url('admin/admin/state')}`,
                                data: {
                                    state: 1,
                                    id: ids
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code == 0) {
                                        lightyear.loading('show');
                                        setTimeout(function () {
                                            lightyear.loading('hide');
                                            // $('.state').addClass('text-danger').removeClass('text-success');
                                            lightyear.notify(res.msg, 'success', 3000);
                                            // lightyear.url('admin/admin/index');
                                            location.href = `{:url('admin/admin/index')}`;
                                        }, 1e3)
                                    } else {
                                        lightyear.notify(res.msg, 'success', 1000);
                                    }
                                }
                            })
                            // $.alert('你点击了确认!');
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            // $.alert('你点击了取消!');
                        }
                    }
                }
            });
        });
        // 禁用
        $('.btn-warning').on('click', function () {
            $.alert({
                title: '修改管理员状态',
                content: '确认禁用',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {
                            // 调用获取id方法
                            var ids = getID();

                            $.ajax({
                                type: 'post',
                                url: `{:url('admin/admin/state')}`,
                                data: {
                                    state: 0,
                                    id: ids
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code == 1) {
                                        lightyear.loading('show');
                                        setTimeout(function () {
                                            lightyear.loading('hide');
                                            // $('.state').addClass('text-danger').removeClass('text-success');
                                            lightyear.notify(res.msg, 'success', 3000);
                                            // lightyear.url('admin/admin/index');
                                            location.href = `{:url('admin/admin/index')}`;
                                        }, 1e3)
                                    } else {
                                        lightyear.notify(res.msg, 'success', 1000);
                                    }
                                }
                            })
                            // $.alert('你点击了确认!');
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            // $.alert('你点击了取消!');
                        }
                    }
                }
            });
        });
        // 删除多条
        $('.btn-danger').on('click', function () {
            $.alert({
                title: '是否删除用户',
                content: '确认删除？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {
                            // 调用获取id的方法
                            var ids = getID();

                            $.ajax({
                                type: 'post',
                                url: `{:url('admin/admin/delete')}`,
                                data: {
                                    state: 1,
                                    id: ids
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code == 1) {
                                        lightyear.loading('show');
                                        setTimeout(function () {
                                            lightyear.loading('hide');
                                            // $('.state').addClass('text-danger').removeClass('text-success');
                                            lightyear.notify(res.msg, 'success', 1500);
                                            // lightyear.url('admin/admin/index');
                                            setTimeout(() => {

                                                location.href = `{:url('admin/admin/index')}`;
                                            }, 1700);
                                        }, 1e3)
                                    } else {
                                        lightyear.notify(res.msg, 'warning', 1500);
                                    }
                                }
                            })
                            // $.alert('你点击了确认!');
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            // $.alert('你点击了取消!');
                        }
                    }
                }
            });
        })
        // 删除单条
        $('.delete').on('click', function () {
            var that = $(this);
            $.alert({
                title: '是否删除用户',
                content: '确认删除？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {

                            var id = that.data('id');

                            $.ajax({
                                type: 'post',
                                url: `{:url('admin/admin/delete')}`,
                                data: {
                                    // state: 1,
                                    id: id
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code == 1) {
                                        lightyear.loading('show');
                                        setTimeout(function () {
                                            lightyear.loading('hide');
                                            // $('.state').addClass('text-danger').removeClass('text-success');
                                            lightyear.notify(res.msg, 'success', 1500);
                                            // lightyear.url('admin/admin/index');
                                            setTimeout(() => {

                                                location.href = `{:url('admin/admin/index')}`;
                                            }, 1700);
                                        }, 1e3)
                                    } else {
                                        lightyear.notify(res.msg, 'warning', 1500);
                                    }
                                }
                            })
                            // $.alert('你点击了确认!');
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            // $.alert('你点击了取消!');
                        }
                    }
                }
            });
        })
        // 删除单条
        $('.btn-del').on('click', function () {
            var that = $(this);
            $.alert({
                title: '是否删除用户',
                content: '确认删除？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-primary',
                        action: function () {

                            var id = that.data('id');

                            $.ajax({
                                type: 'post',
                                url: `{:url('admin/admin/del')}`,
                                data: {
                                    // state: 1,
                                    id: id
                                },
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code == 1) {
                                        lightyear.loading('show');
                                        setTimeout(function () {
                                            lightyear.loading('hide');
                                            // $('.state').addClass('text-danger').removeClass('text-success');
                                            lightyear.notify(res.msg, 'success', 1500);
                                            // lightyear.url('admin/admin/index');
                                            setTimeout(() => {

                                                location.href = `{:url('admin/admin/index')}`;
                                            }, 1700);
                                        }, 1e3)
                                    } else {
                                        lightyear.notify(res.msg, 'warning', 1500);
                                    }
                                }
                            })
                            // $.alert('你点击了确认!');
                        }
                    },
                    cancel: {
                        text: '取消',
                        action: function () {
                            // $.alert('你点击了取消!');
                        }
                    }
                }
            });
        })

    });
</script>